﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class UomDA : IUomDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string SQL_CHECK_UOM_ID_UNIQUE = " SELECT COUNT(1) FROM @_@UOM WHERE UOM_ID = @UOM_ID ";
        private string SQL_DELETE_UOM = " DELETE FROM @_@UOM WHERE UOM_ID = @UOM_ID AND SYSTEM_CREATED = 'N'";
        private string SQL_INSERT_UOM = " INSERT INTO @_@UOM ( WH_ID, UOM_ID, UOM_DESCR, UOM_TYPE, IS_ACTIVE, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @UOM_ID, @UOM_DESCR, @UOM_TYPE, @IS_ACTIVE, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_UOM = "SELECT  uom.ROWID, uom.WH_ID, uom.UOM_ID, uom.UOM_DESCR,  uom.UOM_TYPE ,   T.[TEXT] AS T_TYPE,  uom.IS_ACTIVE, uom.SYSTEM_CREATED, uom.TS, uom.CREATED_BY, uom.CREATED_DATE, uom.UPDATED_BY, uom.UPDATED_DATE    FROM  @_@UOM as uom  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = uom.UOM_TYPE AND SCD.SYS_CODE_TYPE = 'uom'   LEFT JOIN sysadm.TRANSLATION T ON 'Uom.'+ uom.UOM_TYPE = T.ITEM_ID     AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_UOM = " UPDATE @_@UOM SET WH_ID = @WH_ID, UOM_DESCR = @UOM_DESCR, UOM_TYPE = @UOM_TYPE, IS_ACTIVE = @IS_ACTIVE, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE UOM_ID = @UOM_ID  ";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string TS = "@TS";
        private string UOM_DESCR = "@UOM_DESCR";
        private string UOM_ID = "@UOM_ID";
        private string UOM_TYPE = "@UOM_TYPE";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckUomIDUnique(string uomID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UOM_ID", uomID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_UOM_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckUomIDUnique(DataBase dataBase, DbTransaction tran, string uomID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UOM_ID", uomID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_UOM_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteUom(List<string> uomID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < uomID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UOM_ID", uomID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_UOM.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteUom(DataBase dataBase, DbTransaction tran, List<string> uomID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < uomID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UOM_ID", uomID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_UOM.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllUom(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_UOM.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllUom(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_UOM.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public UomInfo GetUomByID(string uomID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_UOM.Replace("@_@", whLoginID) + " WHERE UOM_ID = @UOM_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UOM_ID", uomID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            UomInfo uom = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    uom = new UomInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["UOM_ID"].ToString(), reader["UOM_DESCR"].ToString(), reader["UOM_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["T_TYPE"].ToString());
                }
            }
            return uom;
        }

        public UomInfo GetUomByID(DataBase dataBase, DbTransaction tran, string uomID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_UOM.Replace("@_@", whLoginID) + " WHERE UOM_ID = @UOM_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UOM_ID", uomID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            UomInfo uom = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                uom = new UomInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["UOM_ID"].ToString(), reader["UOM_DESCR"].ToString(), reader["UOM_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["T_TYPE"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return uom;
        }

        public DataSet GetUomByQueryList(List<string> sqlWhere, UomQueryEntity uomQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_UOM.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!uomQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, uomQuery.CurrentPage, uomQuery.PageSize, uomQuery.SortField, uomQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetUomByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, UomQueryEntity uomQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_UOM.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!uomQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, uomQuery.CurrentPage, uomQuery.PageSize, uomQuery.SortField, uomQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertUom(UomInfo uom, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    uom.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Uom_Parameters(uom);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_UOM.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertUom(DataBase dataBase, DbTransaction tran, UomInfo uom, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    uom.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Uom_Parameters(uom);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_UOM.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Uom_Parameters(UomInfo uomInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.UOM_ID, SqlDbType.VarChar, 20), new SqlParameter(this.UOM_DESCR, SqlDbType.VarChar, 50), new SqlParameter(this.UOM_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 0x12), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 0x12) };
            if (!string.IsNullOrEmpty(uomInfo.WhID))
            {
                paramArray[0].Value = uomInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = uomInfo.UomID;
            if (!string.IsNullOrEmpty(uomInfo.UomDescr))
            {
                paramArray[2].Value = uomInfo.UomDescr;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = uomInfo.UomType;
            paramArray[4].Value = uomInfo.IsActive;
            paramArray[5].Value = uomInfo.SystemCreated;
            paramArray[6].Value = uomInfo.CreatedBy;
            paramArray[7].Value = uomInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateUom(UomInfo uom, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    uom.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Uom_Parameters(uom);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_UOM.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateUom(DataBase dataBase, DbTransaction tran, UomInfo uom, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    uom.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Uom_Parameters(uom);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_UOM.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

